import codecs
import os

import xlrd

import numpy as np
import pandas as pd
import sys

print(sys.path)
sys.path.append("c:\\users\\newuser\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages")

from test import predict
import Levenshtein


def compare():
    ExcelFile = pd.read_excel('E:/数据处理/compare1508.xlsx', header=None, index=None).fillna(0)
    y = np.array(ExcelFile.values)
    row = y.shape[0]
    fw = codecs.open('data/testTrain1500-1502.txt', 'w', 'utf-8')
    # 只输出结果
    # for i in range(0, row):
    #     resultAction, resultTarget, resultData = predict(y[i, 0])
    #     line = ''.join(str(resultAction) + ',' + str(resultTarget) + ',' + str(resultData)) + '\n'
    #     fw.writelines(line)
    # fw.close()
    # 计算距离
    num = 0
    for i in range(0, row):
        print(y[i, 0])
        resultAction, resultTarget, resultData = predict(y[i, 0])
        initAcion = str(y[i, 1]).replace("，", "")
        initTarget = str(y[i, 2]).replace("，", "")
        initData = str(y[i, 3]).replace("，", "")

        resultAction = str(resultAction).replace('***','')
        resultTarget = str(resultTarget).replace('***','')
        resultData = "".join(resultData).replace('***','')

        Aratio = Levenshtein.ratio(initAcion, str(resultAction))
        Tratio = Levenshtein.ratio(initTarget, str(resultTarget))
        Dratio = Levenshtein.ratio(initData, str(resultData))
        # print(Aratio, Tratio)
        line = ''.join(
            str(y[i, 0]) + '\n' + 'action:' + initAcion + '\t' + 'pAction:' + str(resultAction) + '\t' + str(
                Aratio) + '\n' + 'target:' + initTarget + '\n' + 'pTarget:' + str(resultTarget) + '\n' + str(
                Tratio) + '\n' + 'data:' + initData + '\n' + 'pData:' + str(resultData) + '\n' + str(Dratio)) + '\n'
        fw.writelines(line)
        if (resultAction in initAcion) and (resultTarget in initTarget) or Tratio >0.8:
            num = num + 1
        else:
            print(line)

    fw.close()
    result = num / row
    print('result:' + str(result))


compare()
